// pages/apply.js
var util = require('../../utils/util.js')
const db = wx.cloud.database().collection('apply')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tip: false,
    tip_content: '',
    type_array: ['非离校请假', '离校不离市请假', '离市不离省请假', '离省请假'],
    type_index: '',
    type_default_msg: '请选择外出类型',
    isshow: false,
    multiArray: [
      ['大学城校区', '三元里校区'],
      ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M'],
      ['1', '2', '3', '4', '5', '6', '7'],
      ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
    ],
    multiIndex: [0, 0, 0, 0],
    local_default_msg: '请选择所在宿舍',
    list: [{
        e_name: 'lfd',
        name: "请假日期",
        type: 'date',
        child_type: 'u-left',
        date: util.formatDate(new Date()),
        start: util.formatDate(new Date()),
        end: util.getend(30),
        date_index: 0,
        default_msg: '请选择请假日期'
      }, {
        e_name: 'lft',
        name: "请假时间",
        type: 'time',
        child_type: 'u-right',
        date: util.formatTime_a(new Date()),
        start: util.formatTime_a(new Date()),
        end: '23:59',
        date_index: 1,
        default_msg: '请选择请假时间'
      }, {
        e_name: 'bfd',
        name: "结假日期",
        type: 'date',
        child_type: 'u-left',
        date: util.formatDate(new Date()),
        start: util.formatDate(new Date()),
        end: '',
        date_index: 2,
        default_msg: '请选择结假日期'
      },
      {
        e_name: 'bft',
        name: "结假时间",
        type: 'time',
        child_type: 'u-right',
        date: util.formatTime_a(new Date()),
        start: '',
        end: '23:59',
        date_index: 3,
        default_msg: '请选择结假时间'
      }
    ],

  },

  bindPickerChange: function (e) {

    if (e.detail.value == 0) {
      this.setData({
        type_index: e.detail.value,
        isshow: false
      })
    } else {
      this.setData({
        type_index: e.detail.value,
        isshow: true
      })
    }
    console.log(e.detail.value)
    console.log(this.data.isshow)
  },
  bindMultiPickerColumnChange: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      local_default_msg: '',
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        switch (data.multiIndex[0]) {
          case 0:
            data.multiArray[1] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', '国际楼'];
            data.multiArray[2] = ['1', '2', '3', '4', '5', '6', '7'];
            data.multiArray[3] = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10'];
            break;
          case 1:
            data.multiArray[1] = ['1', '2', '2A', '3', '3B', '5', '新北楼'];
            data.multiArray[2] = ['1', '2', '3', '4', '5', '6'];
            data.multiArray[3] = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
            break;
        }
        data.multiIndex[1] = 0;
        data.multiIndex[2] = 0;
        break;
      case 1:
        switch (data.multiIndex[0]) {
          case 0:
            switch (data.multiIndex[1]) {
              case 12:
                data.multiArray[2] = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
                data.multiArray[3] = ['01', '02', '03', '04', '05', '06'];
                break;
              default:
                data.multiArray[2] = ['1', '2', '3', '4', '5', '6', '7'];
                data.multiArray[3] = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10'];
                break;
            }
            break;
          case 1:
            switch (data.multiIndex[1]) {
              case 6:
                data.multiArray[2] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
                data.multiArray[3] = ['01', '02', '03', '04', '05', '06', '07', '08']
                break;
              default:
                data.multiArray[2] = ['1', '2', '3', '4', '5', '6'];
                data.multiArray[3] = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
                break;
            }
            break;
        }
        data.multiIndex[2] = 0;
        console.log(data.multiIndex);
        break;
    }
    this.setData(data);
  },
  bindMultiPickerChange: function (e) {

    var data = {
      local_default_msg: '',
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    }
    data.multiIndex[e.detail.column] = e.detail.value;
    this.setData(data);
    console.log(11111)
  },
  bindDateChange: function (e) {
    console.log(e.target.dataset.index)
    var arr = this.data.list
    var index = e.target.dataset.index
    if (arr[index].default_msg) {
      arr[index].default_msg = ''
    }
    arr[index].date = e.detail.value
    if (index == 0) {
      arr[2].date = arr[index].date
      arr[2].start = arr[index].date
    }
    if (arr[0].date == util.formatDate(new Date())) {
      arr[1].start = util.formatTime_a(new Date())
      arr[1].end = "23:59"
    } else {
      arr[1].start = '00:00'
      arr[1].end = "23:59"
    }
    if (index == 1) {
      arr[3].date = arr[index].date
      if (arr[0].date == arr[2].date) {
        arr[3].start = arr[index].date
      }
    }
    if (index == 0 || index == 2) {
      arr[3].date = arr[1].date
      if (arr[2].date == util.formatDate(new Date())) {
        arr[3].start = util.formatTime_a(new Date())
      } else if (arr[0].date == arr[2].date) {
        arr[3].start = arr[1].date
      } else {
        arr[3].start = '00:00'
      }
    }

    this.setData({
      list: arr
    })
  },
  formSubmit(e) {
    const that = this
    wx.showModal({
      title: '提示',
      content: '确认信息填写无误吗？',
      success: function (res) {
        if (res.confirm) { //这里是点击了确定以后
          console.log('用户点击确定')
          var user = wx.getStorageSync('user')
          var type = e.detail.value.type;

          var _local = that.data.multiArray[0][e.detail.value.local[0]] +
          that.data.multiArray[1][e.detail.value.local[1]] + that.data.multiArray[2][e.detail.value.local[2]] + that.data.multiArray[3][e.detail.value.local[3]]
          var lfd = e.detail.value.lfd
          var lft = e.detail.value.lft
          var bfd = e.detail.value.bfd
          var bft = e.detail.value.bft
          var date = ''
          var long=lfd+'/'+lft+'至'+bfd+'/'+bft
          var hour = lft + '-' + bft
          var isoneday=true
          if (lfd == bfd) {
            date = lfd
          } else {
            date = lfd + ' 至 ' + bfd
            isoneday=false
          }
          var contact = e.detail.value.contact
          var reason = e.detail.value.reason
          var way = e.detail.value.way

          if (!type || !_local || !lfd || !lft || !bfd || !bft || !contact || !reason) {
            that.tip("请完善信息！")
            return;
          }
          if (!way && that.data.isshow) {
            that.tip("请完善信息！")
            return;
          }

          console.log('user:' + user)
          var _time = util.formatTime(new Date())
          var name=wx.getStorageSync('name')
          var grade=wx.getStorageSync('grade')
          db.add({
            data: {
              stu: user,
              date: date,
              hour: hour,
              reason: type,
              status: '审批中',
              time: _time,
              contact: contact,
              way: way,
              detail: reason,
              local: _local,
              isoneday:isoneday,
              long:long,
              name:name,
              grade:grade
            }
          }).then(res => {
            that.tip("申请成功！")
            setTimeout(function () {
              wx.navigateBack({
                delta: 0,
              })
            }, 1000)
          })
        } else { //这里是点击了取消以后
          console.log('用户点击取消')
        }
      }
    })

  },
  tip: function (str) {
    const that = this

    that.setData({
      tip_content: str,
      tip: true
    })
    setTimeout(function () {
      that.setData({
        tip: false
      })
    }, 1000)
  },
})